﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class HideSystem : IExternalCommand
	{
		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			this._uiDoc = commandData.Application.ActiveUIDocument;
			this._doc = this._uiDoc.Document;
			Transaction transaction = null;
			if (HideSystem.hWndRevit == null)
			{
				HideSystem.hWndRevit = new YJKWindowHandle(Process.GetCurrentProcess().MainWindowHandle);
			}
			List<ViewType> list = new List<ViewType>();
            list.Add((ViewType)1);
            list.Add((ViewType)4);
            list.Add((ViewType)2);
            list.Add((ViewType)117);
            list.Add((ViewType)10);
			if (!YJKRevitTools.ViewCheck(this._uiDoc, list))
			{
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			int wMsg = 7;
			HideSystem.SendMessage(HideSystem.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			HideShowElement hideShowElement = new HideShowElement(this._doc, this._uiDoc);
			Result result;
			try
			{
				SystemHideDialog systemHideDialog = new SystemHideDialog();
				systemHideDialog.StartPosition = FormStartPosition.CenterScreen;
				DialogResult dialogResult = systemHideDialog.ShowDialog(HideSystem.hWndRevit);
				for (;;)
				{
					transaction = new Transaction(this._doc, "HideSystem");
					transaction.Start();
					if (dialogResult == DialogResult.Cancel)
					{
						goto IL_169;
					}
					if (systemHideDialog.isShow)
					{
						if (systemHideDialog.isSystem)
						{
							break;
						}
						if (systemHideDialog.isSpinkLer)
						{
							goto Block_9;
						}
					}
					else
					{
						if (systemHideDialog.isSystem)
						{
							hideShowElement.hideSystem();
						}
						if (systemHideDialog.isSpinkLer)
						{
							goto Block_11;
						}
					}
					this._doc.Regenerate();
					transaction.Commit();
				}
				hideShowElement.showSystem();
				transaction.Commit();
				return Autodesk.Revit.UI.Result.Succeeded;
				Block_9:
				hideShowElement.showLerHide();
				transaction.Commit();
				return Autodesk.Revit.UI.Result.Succeeded;
				Block_11:
				hideShowElement.spinkLerHide();
				transaction.Commit();
				return Autodesk.Revit.UI.Result.Succeeded;
				IL_169:
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				ex.ToString();
				result = Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				if (transaction != null && (int)transaction.GetStatus() != 3)
				{
					transaction.RollBack();
				}
			}
			return result;
		}

		private UIDocument _uiDoc;

		private Document _doc;

		private List<ElementId> eles = new List<ElementId>();

		public static YJKWindowHandle hWndRevit;
	}
}
